import toolMgr from '@/biz/base/tool.js'
import dt from '@dt/dt'
import ruleOpt from '@/biz/option/rule.js'

let _this = null

function accessory(info) {
  let files = []
  if (info.accessoryBOList) {
    files = info.accessoryBOList.map(item => {
      return {
        value: item.accessoryName,
        key: item.accessoryUrl
      }
    })
  }
  return [{
    title: '代理合同附件',
    tag: {
      closable: false,
      options: files,
      color: 'blue',
      click: (tag) => {
        toolMgr.downloadFile(tag.key, tag.value)
      }
    }
  }]
}

function part1() {
  return [
    [{
      title: '案场名称',
      key: 'housesName',
      text: {}
    }, {
      title: '产品',
      key: 'productNames',
      text: {}
    }],
    [{
      title: '佣金规则名称',
      key: 'ruleName',
      text: {}
    }, {
      title: '结佣参考价',
      key: 'refPriceStr',
      text: {}
    }],
    [{
      title: '基础代理费率',
      text: {},
      data: {
        from: (info) => {
          return `${info.baseAgentTypeStr} ${info.baseAgentCost}` + (info.baseAgentType == 1 ? '%' : '元')
        }
      }
    }, {
      title: '结佣方式',
      key: 'commissionWayStr',
      text: {}
    }]
  ]
}

function part2(type) {
  let time = {
    title: '有效期',
    text: {},
    data: {
      from: (info) => {
        return info.startDate.split(' ')[0] + ' ~ ' + info.endDate.split(' ')[0]
      }
    }
  }
  let way = {
    title: '结佣生效方式',
    key: 'validWayStr',
    text: {}
  }
  let arr1 = [{
    title: '交房保证金比例',
    text: {},
    data: {
      from: (info) => {
        return info.depositPer + '%'
      }
    }
  }]
  let arr2 = []
  if (_this.info.commissionWay == '2') {
    arr1.push({
      title: '签约比例',
      text: {},
      data: {
        from: (info) => {
          return info.contractPer + '%'
        }
      }
    })
    arr2.push({
      title: '签约扣除保证金',
      text: {},
      data: {
        from: (info) => {
          return info.isDeductDeposit ? '是' : '否'
        }
      }
    })
  }

  let arr = [arr1, arr2]
  arr.push([way, time])
  return arr
}

function audit() {
  return [
    [{
      title: '审核结果',
      key: 'type',
      placeholder: '请审核',
      select: {
        options: [{
          key: 2,
          value: '通过'
        }, {
          key: 3,
          value: '驳回'
        }]
      },
      associationRules: ['auditContent'],
      change: () => {
        _this.update()
      },
      rules: [
        ruleOpt.required('请审核')
      ]
    }],
    content(_this.info)
  ]
}

function content(info) {
  let opt = {
    title: '审核内容',
    key: 'auditContent',
    input: {
      type: 'textarea',
      autosize: {
        minRows: 2
      }
    }
  }
  if (info.type == 3) {
    opt.rules = [
      ruleOpt.required('请输入审核内容'),
      ruleOpt.max(300)
    ]
  }
  return [opt]
}

function detail() {
  return [
    [{
      title: '审核结果',
      key: 'ruleStatusStr',
      text: {
        color: _this.info.ruleStatus == 3 ? 'red' : null
      }
    }],
    [{
      title: '审核内容',
      placeholder: '-',
      text: {},
      key: 'auditContent',
    }],
    [{
      title: '审核人',
      placeholder: '-',
      key: 'auditName',
      text: {}
    }, {
      title: '审核时间',
      placeholder: '-',
      text: {},
      data: {
        from: (info) => {
          return info.auditTime
        }
      }
    }],
  ]
}

function options(vm, type = 1) { // 1 detail 2 audit 3 audit-detail
  _this = vm
  let end = type == 2 ? audit() : detail()
  return [
    ...part1(),
    ...part2(type),
    accessory(_this.info),
    [{
      title: '备注',
      key: 'remark',
      text: {}
    }],
    [{
      title: '提交人',
      key: 'submitterName',
      text: {}
    }, {
      title: '提交时间',
      text: {},
      data: {
        from: (info) => {
          return info.submitterTime
        }
      }
    }],
    ...end
  ]
}

export default {
  options
}